<template>
  <Card title="级别占比" :loading="loading">
    <div ref="chartRef" :style="{ width, height }"></div>
  </Card>
</template>
<script lang="ts" setup>
  import { onMounted, Ref, ref, watch } from 'vue';
  import { Card } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { StdResponse } from '/@/api/model/baseModel';
  import { getAlarmLogsCount } from '/@/api/monitor/rule';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { GetAlarmCount } from '/@/api/monitor/entity/entity';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '300px',
    },
  });

  const countData = ref<GetAlarmCount>([]);

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  const { createMessage } = useMessage();

  const loadData = async () => {
    const rsp: StdResponse = await getAlarmLogsCount({ length: 7 * 24, is_pie: 'true' });
    if (!rsp) {
      createMessage.error('获取数据失败');
    }
    let data: GetAlarmCount = rsp;
    countData.value = data;
  };

  onMounted(() => {
    loadData();
  });

  watch(
    () => props.loading,
    () => {
      if (props.loading) {
        return;
      }
      setOptions({
        tooltip: {
          trigger: 'item',
        },

        series: [
          {
            name: '告警级别',
            type: 'pie',
            radius: '80%',
            center: ['50%', '50%'],
            color: ['#5ab1ef', '#b6a2de', '#67e0e3'],
            data: countData.value.sort(function (a, b) {
              return a.value - b.value;
            }),
            roseType: 'radius',
            animationType: 'scale',
            animationEasing: 'exponentialInOut',
            animationDelay: function () {
              return Math.random() * 400;
            },
          },
        ],
      });
    },
    { immediate: true },
  );
</script>
